home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / widgets-mesa / demos / cube.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-31  |  8.8 KB  |  366 lines

  1. /* cube.c -- Demo program for the Mesa widget
  2.    Copyright (C) 1995, 1996 Thorsten.Ohl @ Physik.TH-Darmstadt.de
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    $Id: cube.c,v 1.20 1996/09/30 00:21:06 ohl Exp $
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <math.h>
  25. #include <X11/X.h>
  26. #include <X11/Intrinsic.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Shell.h>
  29. #ifndef __GLX_MOTIF
  30. #include <X11/Xaw/Command.h>
  31. #include <X11/Xaw/Form.h>
  32. #include <GL/xmesa.h>
  33. #include <GL/gl.h>
  34. #include <GL/GLwDrawA.h>
  35. #else /* __GLX_MOTIF */
  36. #include <Xm/PushB.h>
  37. #include <Xm/Form.h>
  38. #include <GL/xmesa.h>
  39. #include <GL/gl.h>
  40. #include <GL/GLwMDrawA.h>
  41. #endif /* __GLX_MOTIF */
  42.  
  43. static char *RCS_Id =
  44. "@(#) $Id: cube.c,v 1.20 1996/09/30 00:21:06 ohl Exp $";
  45.  
  46. static GLint Black, Red, Green, Blue;
  47.  
  48. void quit_function (Widget, XtPointer, XtPointer);
  49.  
  50. void
  51. quit_function (Widget w, XtPointer closure, XtPointer call_data)
  52. {
  53.   exit (0);
  54. }
  55.  
  56. static void
  57. draw_cube (void)
  58. {
  59.   /* X faces */
  60.   glIndexi (Red);
  61.   glColor3f (1.0, 0.0, 0.0);
  62.   glBegin (GL_POLYGON);
  63.   {
  64.     glVertex3f (1.0, 1.0, 1.0);
  65.     glVertex3f (1.0, -1.0, 1.0);
  66.     glVertex3f (1.0, -1.0, -1.0);
  67.     glVertex3f (1.0, 1.0, -1.0);
  68.   }
  69.   glEnd ();
  70.  
  71.   glBegin (GL_POLYGON);
  72.   {
  73.     glVertex3f (-1.0, 1.0, 1.0);
  74.     glVertex3f (-1.0, 1.0, -1.0);
  75.     glVertex3f (-1.0, -1.0, -1.0);
  76.     glVertex3f (-1.0, -1.0, 1.0);
  77.   }
  78.   glEnd ();
  79.  
  80.   /* Y faces */
  81.   glIndexi (Green);
  82.   glColor3f (0.0, 1.0, 0.0);
  83.   glBegin (GL_POLYGON);
  84.   {
  85.     glVertex3f (1.0, 1.0, 1.0);
  86.     glVertex3f (1.0, 1.0, -1.0);
  87.     glVertex3f (-1.0, 1.0, -1.0);
  88.     glVertex3f (-1.0, 1.0, 1.0);
  89.   }
  90.   glEnd ();
  91.  
  92.   glBegin (GL_POLYGON);
  93.   {
  94.     glVertex3f (1.0, -1.0, 1.0);
  95.     glVertex3f (-1.0, -1.0, 1.0);
  96.     glVertex3f (-1.0, -1.0, -1.0);
  97.     glVertex3f (1.0, -1.0, -1.0);
  98.   }
  99.   glEnd ();
  100.  
  101.   /* Z faces */
  102.   glIndexi (Blue);
  103.   glColor3f (0.0, 0.0, 1.0);
  104.   glBegin (GL_POLYGON);
  105.   {
  106.     glVertex3f (1.0, 1.0, 1.0);
  107.     glVertex3f (-1.0, 1.0, 1.0);
  108.     glVertex3f (-1.0, -1.0, 1.0);
  109.     glVertex3f (1.0, -1.0, 1.0);
  110.   }
  111.   glEnd ();
  112.  
  113.   glBegin (GL_POLYGON);
  114.   {
  115.     glVertex3f (1.0, 1.0, -1.0);
  116.     glVertex3f (1.0, -1.0, -1.0);
  117.     glVertex3f (-1.0, -1.0, -1.0);
  118.     glVertex3f (-1.0, 1.0, -1.0);
  119.   }
  120.   glEnd ();
  121. }
  122.  
  123. GLfloat xrot, yrot, zrot;
  124.  
  125. static void
  126. init_display (Widget w)
  127. {
  128.   xrot = yrot = zrot = 0.0;
  129.  
  130.   glClearColor (0.0, 0.0, 0.0, 0.0);
  131.   glClearIndex (Black);
  132.   glClearDepth (10.0);
  133.  
  134.   glMatrixMode (GL_PROJECTION);
  135.   glLoadIdentity ();
  136.   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  137.   glTranslatef (0.0, 0.0, -3.0);
  138.  
  139.   glMatrixMode (GL_MODELVIEW);
  140.   glLoadIdentity ();
  141.  
  142.   glCullFace (GL_BACK);
  143.   glEnable (GL_CULL_FACE);
  144.  
  145.   glShadeModel (GL_FLAT);
  146. }
  147.  
  148. static void
  149. display (Widget w)
  150. {
  151.   glClear (GL_COLOR_BUFFER_BIT);
  152.   glPushMatrix ();
  153.   {
  154.     glRotatef (xrot, 1.0, 0.0, 0.0);
  155.     glRotatef (yrot, 0.0, 1.0, 0.0);
  156.     glRotatef (zrot, 0.0, 0.0, 1.0);
  157.     draw_cube ();
  158.   }
  159.   glPopMatrix ();
  160.   glFinish ();
  161.   glXSwapBuffers (XtDisplay (w), XtWindow (w));
  162.       
  163.   xrot += 1.0;
  164.   yrot += 0.7;
  165.   zrot -= 0.3;
  166. }
  167.  
  168.  
  169. static GLint
  170. alloc_color (Widget w, Colormap cmap, int red, int green, int blue)
  171.   XColor xcolor;
  172.   xcolor.red = red;
  173.   xcolor.green = green;
  174.   xcolor.blue = blue;
  175.   xcolor.flags = DoRed | DoGreen | DoBlue;
  176.   if (!XAllocColor (XtDisplay (w), cmap, &xcolor))
  177.     {
  178.       printf ("Couldn't allocate color!\n");
  179.       exit (1);
  180.     }
  181.   return xcolor.pixel;
  182. }
  183.  
  184. /* This is rather inefficient, but we don't mind for the moment,
  185.    because it works.  */
  186.  
  187. static void
  188. translate_pixels (Widget to, Widget from, ...)
  189. {
  190.   va_list ap;
  191.   char *name;
  192.   Colormap from_cmap, to_cmap;
  193.   XColor xcolor;
  194.  
  195.   XtVaGetValues (from, XtNcolormap, &from_cmap, NULL);
  196.   XtVaGetValues (to, XtNcolormap, &to_cmap, NULL);
  197.  
  198.   va_start (ap, from);
  199.   for (name = va_arg (ap, char *); name != NULL; name = va_arg (ap, char *))
  200.     {
  201.       XtVaGetValues (from, name, &xcolor.pixel, NULL);
  202.       XQueryColor (XtDisplay (from), from_cmap, &xcolor);
  203.       if (!XAllocColor (XtDisplay (to), to_cmap, &xcolor))
  204.     XtAppWarning (XtWidgetToApplicationContext (to),
  205.               "Couldn't allocate color!\n");
  206.       else
  207.     XtVaSetValues (from, name, xcolor.pixel, NULL);
  208.     }
  209.   va_end (ap);
  210. }
  211.  
  212. /* Just like the movies: do 24 frames per second. */
  213. unsigned long delay = 1000/24;
  214.  
  215. static void first_frame (Widget);
  216. static void next_frame (XtPointer, XtIntervalId *);
  217.  
  218. static void
  219. first_frame (Widget w)
  220. {
  221.   XtAppAddTimeOut (XtWidgetToApplicationContext (w),
  222.               delay, next_frame, (XtPointer) w);
  223. }
  224.  
  225. static void
  226. next_frame (XtPointer client_data, XtIntervalId *id)
  227. {
  228.   Widget w = (Widget) client_data;
  229.   first_frame (w);
  230.   display (w);
  231. }
  232.  
  233. static String fallback_resources[] =
  234. {
  235. #ifndef __GLX_MOTIF
  236.   "*GLwDrawingArea.width: 300",
  237.   "*GLwDrawingArea.height: 300",
  238.   "*GLwDrawingArea.rgba: true",
  239.   "*GLwDrawingArea.installColormap: true",
  240.   "*GLwDrawingArea.doublebuffer: true",
  241. #else /* __GLX_MOTIF */
  242.   "*GLwMDrawingArea.width: 300",
  243.   "*GLwMDrawingArea.height: 300",
  244.   "*GLwMDrawingArea.rgba: true",
  245.   "*GLwMDrawingArea.installColormap: true",
  246.   "*GLwMDrawingArea.doublebuffer: true",
  247. #endif /* __GLX_MOTIF */
  248.   NULL
  249. };
  250.  
  251. int
  252. main (int argc, char *argv[])
  253. {
  254.   Widget top, frame, mesa, quit;
  255.   XtAppContext app_context;
  256.   XVisualInfo *vi;
  257.   Boolean rgba, doublebuffer, cmap_installed;
  258.  
  259.   XtSetLanguageProc (NULL, NULL, NULL);
  260.   top = XtVaAppInitialize (&app_context, "Cube",
  261.                NULL, 0,
  262.                &argc, argv, fallback_resources,
  263.                NULL);
  264. #ifndef __GLX_MOTIF
  265.   frame = XtVaCreateManagedWidget ("frame", formWidgetClass,
  266.                    top,
  267.                    NULL);
  268.   mesa = XtVaCreateManagedWidget ("mesa", glwDrawingAreaWidgetClass,
  269.                   frame,
  270.                   NULL);
  271.   quit = XtVaCreateManagedWidget ("quit", commandWidgetClass,
  272.                   frame,
  273.                                   XtNfromHoriz, mesa, XtNhorizDistance, 10,
  274.                   NULL);
  275.   XtAddCallback (quit, XtNcallback, quit_function, NULL);
  276. #else /* __GLX_MOTIF */
  277.   frame = XtVaCreateManagedWidget ("frame", xmFormWidgetClass,
  278.                    top,
  279.                    NULL);
  280.   mesa = XtVaCreateManagedWidget ("mesa", glwMDrawingAreaWidgetClass,
  281.                   frame,
  282.                   XmNtopAttachment, XmATTACH_FORM,
  283.                   XmNtopOffset, 10,
  284.                   XmNleftAttachment, XmATTACH_FORM,
  285.                   XmNleftOffset, 10,
  286.                   XmNrightAttachment, XmATTACH_NONE,
  287.                   XmNbottomAttachment,  XmATTACH_FORM,
  288.                   XmNbottomOffset, 10,
  289.                   NULL);
  290.   quit = XtVaCreateManagedWidget ("quit", xmPushButtonWidgetClass,
  291.                   frame,
  292.                   XmNtopAttachment, XmATTACH_FORM,
  293.                   XmNtopOffset, 10,
  294.                   XmNleftAttachment, XmATTACH_WIDGET,
  295.                   XmNleftOffset, 10,
  296.                   XmNleftWidget, mesa,
  297.                   XmNrightAttachment, XmATTACH_FORM,
  298.                   XmNrightOffset, 10,
  299.                   XmNbottomAttachment, XmATTACH_NONE,
  300.                   NULL);
  301.   XtAddCallback (quit, XmNarmCallback, quit_function, NULL);
  302. #endif /* __GLX_MOTIF */
  303.  
  304.   XtRealizeWidget (top);
  305.  
  306.   XtVaGetValues (mesa,
  307.          GLwNrgba, &rgba,
  308.          GLwNinstallColormap, &cmap_installed,
  309.          GLwNdoublebuffer, &doublebuffer,
  310.          GLwNvisualInfo, &vi,
  311.          NULL);
  312.  
  313.   /* create a visual context */
  314.   GLwDrawingAreaMakeCurrent (mesa,
  315.                  glXCreateContext (XtDisplay(mesa),
  316.                            vi,
  317.                            NULL,
  318.                            GL_FALSE));
  319.  
  320.   if (rgba)
  321.     {
  322.       Black = Red = Green = Blue = 0;
  323.  
  324.       if (cmap_installed)
  325.     {
  326.       /* In RGBA mode, the Mesa widgets will have their own color map.
  327.          Adjust the colors of the other widgets so that--even if the rest
  328.          of the screen has wrong colors--all application widgets have the
  329.          right colors.  */
  330.              
  331.       translate_pixels (mesa, quit,
  332.                 XtNbackground, XtNforeground, XtNborder, NULL);
  333.       translate_pixels (mesa, frame, XtNbackground, XtNborder, NULL);
  334.  
  335.       /* Finally warp the pointer into the mesa widget, to make sure that
  336.          the user sees the right colors at the beginning.  */
  337.  
  338.       XWarpPointer (XtDisplay (mesa), None, XtWindow (mesa),
  339.             0, 0, 0, 0, 0, 0);
  340.     }
  341.     }
  342.   else
  343.     {
  344.       /* Allocate a few colors for use in color index mode.  */
  345.  
  346.       Colormap cmap;
  347.       cmap = DefaultColormap (XtDisplay (top), DefaultScreen (XtDisplay (top)));
  348.       Black = alloc_color (top, cmap, 0x0000, 0x0000, 0x0000);
  349.       Red   = alloc_color (top, cmap, 0xffff, 0x0000, 0x0000);
  350.       Green = alloc_color (top, cmap, 0x0000, 0xffff, 0x0000);
  351.       Blue  = alloc_color (top, cmap, 0x0000, 0x0000, 0xffff);
  352.     }
  353.  
  354.   init_display (mesa);
  355.   first_frame (mesa);
  356.  
  357.   XtAppMainLoop (app_context);
  358.   return (0);
  359. }
  360.  
  361.  
  362.  
  363.  
  364.  
  365.